/ Assembly List / LJCDBClientLib / ObjectManager`2

Namespace - LJCDBClientLib


Syntax

C#
public class ObjectManager<TData, TList>

Provides object specific data methods. (RE)

Remarks

The Object Manager is a generic class that is used by a client application to interface with the contained DataManager object.

The Object Manager converts the Result XML message data into client Data objects. The Data object property names must match those in the Data Definition.

The Add(), Delete(), Load(), Retrieve() and Update() methods take a client Data object as a parameter. They access properties in the Data object using reflection.

The CreateCollection() and CreateData() methods use reflection to populate client Data objects with the result data.

The test code is found in project LJCDBClientLib\TestObjectManager and LJCDataManagerTest.

Public Methods
#ctor Initializes an object instance.
Add Adds a record to the database. (E)
CreateCollection Creates a Data Object collection from the result records.
CreateData Creates a Data Object from the result record.
CreateLoadRequest Creates and returns the Load DbRequest object.
Delete Deletes the records with the specified key values. (E)
ExecuteClientSql Executes a non-query client SQL statement. (E)
ExecuteRequest Execute the supplied request.
GetColumns Creates a set of columns that match the supplied list.
Load Retrieves a collection of data records. (E)
LoadClientSql Executes a "Load" client SQL statement. (E)
LoadProcedure Retrieves a collection of data records.
MapNames Maps the column property and rename values.
Retrieve Retrieves a record from the database. (E)
RetrieveClientSql Executes a "Retrieve" client SQL statement. (E)
SetDbAssignedColumns Sets the database assigned value columns.
SetLookupColumns Adds the lookup column names.
Update Updates the record. (E)
18 Methods

Properties
AffectedCount Gets or sets the non-select affected record count.
BaseDefinition Gets the base data definition columns collection.
DataConfigName Gets or sets the data configuration name.
DataDefinition Gets a reference to the Data Definition columns collection.
DataManager The Data Manager object.
DbServiceRef Gets DbServiceRef object.
SchemaName The Schema name.
SQLStatement Gets or sets the last SQL statement.
TableName The primary table name.

Example

C#
IF NOT EXISTS(SELECT* FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_NAME = N'PersonTest')
BEGIN
CREATE TABLE[dbo].[PersonTest]
(
  [Id][int] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar] (60) NULL,
  [PrincipleFlag] [bit] NOT NULL,
  CONSTRAINT[PK_PersonTest]
  PRIMARY KEY CLUSTERED(
  [Id] ASC),
)
END
GO

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using LJCDBClientLib;
using LJCDBMessage;
using LJCDBDataAccessLib;
using LJCDBServiceLib;

// A Person Data Record class.
public class Person
{
  public Int32 ID { get; set; }
  public string Name { get; set; }
  public bool PrincipleFlag { get; set; }
}

// A collection of Person records.
public class Persons : List<Person> { }

// Test ObjectManager.
public static void TestObjectDataAccess()
{
  DbConnectionStringBuilder connectionBuilder;
  DbDataAccess dbDataAccess;

  string databaseName = "DatabaseName";
  connectionBuilder = new DbConnectionStringBuilder()
  {
    { "Data Source", "DataServiceName" },
    { "Initial Catalog", databaseName },
    { "Integrated Security", "True" }
  };
  string connectionString = connectionBuilder.ConnectionString;
  string providerName = "System.Data.SqlClient";
  dbDataAccess = new DbDataAccess(connectionString, providerName
    , databaseName);

  // Create the Data Access object for the desired communication type.
  //string endPointConfigurationName = "NetTcpBinding_IDbService";
  DbServiceRef dbServiceRef = new DbServiceRef()
  {
    // Direct Message Data Access communication.
    DbDataAccess = dbDataAccess,

    // Direct Database Service.
    //DbService = new DbService(),

    // Remote Database Service with Client Proxy.
    //DbServiceClient = new DbServiceClient(endPointConfigurationName)
  };
    
  // Create the ObjectManager.
  //  - Creates the DataDefinition object.
  //  - Creates the DbRequest object with all data and key columns.
  //  - Sets and returns the database assigned values.
  ObjectManager<Person, Persons> personManager;
  personManager = new ObjectManager<Person, Persons>(dbServiceRef, null
    , "PersonTest");
        
  if (personManager.IsSuccess)
  {
    // Map table names with property names or captions
    // that differ from the column names.
    personManager.MapNames("Id", "ID");

    // Create the list of database assigned columns.
    // And make sure the AutoIncrement value is set.
    personManager.DbAssignedColumnNames = new string[]
    {
      "Id"
    };

    // Create the list of lookup column names.
    // This list must include the database assigned columns.
    personManager.LookupColumnNames = new string[]
    {
      "Id",
      "Name"
    };

    Persons persons = Load(personManager);
    Add(personManager);
    Person person = Retrieve(personManager);
    persons = Load(personManager);
    Update(personManager);
    persons = Load(personManager);
    Delete(personManager);
    persons = Load(personManager);

    Add(personManager);
    person = RetrieveClientSql(personManager);
    persons = LoadClientSql(personManager);
    ExecuteClientSql(personManager);
    persons = LoadClientSql(personManager);
  }
}

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.